<?php
    class controller_index extends __registry_controller
    {
        public $_layout = NULL;

        public $_content_header = 'OPEN';
        public $_content_top = 'OPEN';
        public $_content_bot = NULL;
        public $_content_right = NULL;
        public $_content_left = NULL;
        public $_content_footer = NULL;
        public $arrBlock = NULL;

        public function __construct()
        {
            $this->_layout = NULL;
            parent::add_object(array('handle_validate' , 'handle_string' , 'model_users' , 'model_categories' , 'handle_upload' , 'model_language' , 'handle_permission'));
        }

        public function index_action()
        {

            /* Check permission */
            if ( $this->per_check_permission(NULL , true) ) {

                $this->_['url_add'] = URL_BASE . 'admin/' . $this->route->rt_get_seokey_act('add');
                $this->_['url_edit'] = URL_BASE . 'admin/' . $this->route->rt_get_seokey_act('edit');
                $strhtml = NULL;
                $html = $this->view->view_display("index");
                $this->arrBlock = $this->view->view_get_block_html($html);
                $resultHtml = $this->cat_list(0 , 0 , 1);

                $strhtml = $this->view->view_assign_block($html , array('htmlCat' => $resultHtml));
            } else {
                $this->request->rq_goto(URL_BASE . 'admin/' . $this->route->rt_get_seokey_act('logout'));
            }


            return $strhtml;
        }

        private function cat_list( $id , $indent , $noid = 0 )
        {
            $indent += 1;
            $str = '';
            $strc = '';
            $rr = $this->mdc_cat_list($id , $noid);
            if ( !empty($rr) ) {
                foreach ( $rr as $row ) {
                    $yesid = $this->mdc_get_pcat($row['ID']);

                    $minus = empty($yesid) ? 'minus' : 'plus';

                    $rs = $this->mdc_get_cat_com('Com' , $row['Com']);
                    $seo_com = isset($rs['SeoKey']) ? $rs['SeoKey'] . '/' : Null;
                    $arr_row = array('ID' => $row['ID'] , 'PID' => $row['PID'] , 'minus' => $minus , 'SeoKey' => $row['SeoKey'] , 'link_item' => URL_BASE . 'admin/' . $seo_com . $row['SeoKey'] . '/' , 'indent' => str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;" , $indent) , 'CatName' => $row['CatName'] , 'Order' => $row['Order'] , 'Status' => $row['Status'] , "class_cell" => $row['Status'] == 'show' ? "cell_groupitem$indent" : "cell_groupitem_ex");

                    $html = $id == 0 ? $this->arrBlock['categoryapp'] : $this->arrBlock['category'];

                    $str .= $this->view->view_assign($html , $arr_row);

                    if ( isset($row['ID']) && $str ) {
                        $none = $indent > 1 ? 'none' : 'block';

                        $strc = $this->cat_list($row['ID'] , $indent);
                        $str .= $this->view->view_assign($this->arrBlock['div_child'] , array('ID' => $row['ID'] , 'div_child' => $strc , 'none' => $none));
                    }


                }

            }


            return $str;
        }

        public function add_action()
        {

            /* Check permission */
            if ( !$this->per_check_permission('add' , true) ) {
                $this->_layout = 'error';
                $strhtml = $this->_['not_permission'];

            } else {

                $this->_layout = 'iframe';
                $str_option_com = NULL;
                $this->_['sms'] = '';
                $this->_['dis_none_img'] = 'display:none';
                $this->_['dis_none_iput'] = '';
                $this->_['dis_none'] = '';
                $this->_['path_file_Img'] = NULL;
                $this->_['file_Img'] = URL_BASE . '/upload/noimg.png';

                /* LAY DANH SACH NGON NGU */
                $arr_row_titles = $this->mdl_lang_list(); /* lang max */
                $alang = array(); /* lang data change not empty */
                $arr_lang_insert = array(); /* lang data ok insert */

                /* Kiểm tra lang có dữ liệu */
                if ( !empty($_POST['CatName']) && !empty($arr_row_titles) ) {
                    foreach ( $arr_row_titles as $arr_row ) {
                        $langid = $arr_row['lang_id'];

                        if ( !(empty($_POST['CatName'][$langid]) && empty($_POST['SeoKey'][$langid]) && empty($_POST['Title'][$langid]) && empty($_POST['Desc'][$langid]) && empty($_POST['Keyword'][$langid]) && empty($_POST['TitleH1'][$langid]) && empty($_POST['TitleH2'][$langid]) && empty($_POST['DescF'][$langid]))
                        ) {
                            $alang[] = $langid;
                        }

                    }
                }

                /*Kiểm tra lỗi lang có dữ liệu */
                if ( !empty($_POST['PID']) && !empty($_POST['CatName']) && !empty($_POST['SeoKey']) ) {
                    $add = 0;
                    if ( !empty($alang) ) {
                        foreach ( $alang as $lang_id ) {
                            $_POST['SeoKey'][$lang_id] = $this->str_seokey($_POST['SeoKey'][$lang_id]);
                            $seo_key = $this->mdc_get_seo_key($_POST['SeoKey'][$lang_id] , $lang_id);
                            if ( $seo_key > 0 ) {
                                $this->_['sms'] = $this->_['check_seo_key'] . ' ' . $arr_row['lang_name'];

                            } else if ( empty($_POST['CatName'][$lang_id]) ) {
                                $this->_['sms'] = $this->_['empty_cat_name'] . ' ' . $arr_row['lang_name'];

                            } else if ( empty($_POST['SeoKey'][$lang_id]) ) {

                                $this->_['sms'] = $this->_['empty_seo_key'] . ' ' . $arr_row['lang_name'];

                            } else {

                                /* Upload pic */
                                if ( isset($_FILES['Img']) && !empty($_FILES['Img']['name']) ) {
                                    $img = $this->upload_img($_FILES['Img'] , PATH_BASE . '/upload/images/categories/' , $seo_key);
                                    if ( $img ) {
                                        $_POST['Img'] = $img;
                                    } else {
                                        $this->_['sms'] = $this->_['img_not_format'];
                                    }
                                }
                                $arr_lang_insert[] = $lang_id;
                            }

                        }

                    }
                }

                /* Lưu những lang đã kiểm tra */
                if ( !empty($arr_lang_insert) ) {
                    $add = 0;
                    $id = 0;
                    $done_ok = true;

                    foreach ( $arr_lang_insert as $lagid ) {

                        if ( $add == 0 ) {
                            $id = $this->mdc_cat_insert();
                            $add++;
                            if ( !$id ) {
                                $done_ok = false;
                            }
                        }

                        if ( $id != 0 && $done_ok == true ) {
                            $done = $this->mdc_cat_insert_lang($id , $lagid);

                            if ( $done == false ) {
                                $done_ok = false;
                            }
                        }

                    }

                    /* Điều hướng khi tất cả đều thành công */
                    if ( $done_ok == true ) {
                        $this->request->rq_goto_window_parent(URL_BASE . $this->_seokey[0] . '/');
                    }

                }

                $this->_['title_editor'] = $this->_['title_editor_add'];
                $this->_['val_order'] = (isset($_POST['Order']) ? $_POST['Order'] : 1);
                $this->_['checked_horizontal'] = isset($_POST['HorizontalMenu']) ? 'checked="true"' : NULL;
                $this->_['checked_vertical'] = isset($_POST['HorizontalMenu']) ? 'checked="true"' : NULL;

                if ( !empty($arr_row_titles) ) {
                    foreach ( $arr_row_titles as $arr_row ) {
                        $lang_id = $arr_row['lang_id'];
                        $this->_['CatName_' . $lang_id] = isset($_POST['CatName'][$lang_id]) ? $_POST['CatName'][$lang_id] : NULL;
                        $this->_['SeoKey_' . $lang_id] = isset($_POST['SeoKey'][$lang_id]) ? $_POST['SeoKey'][$lang_id] : NULL;
                        $this->_['xTitle_' . $lang_id] = isset($_POST['Title'][$lang_id]) ? $_POST['Title'][$lang_id] : NULL;
                        $this->_['Desc_' . $lang_id] = isset($_POST['Desc'][$lang_id]) ? $_POST['Desc'][$lang_id] : NULL;
                        $this->_['Keyword_' . $lang_id] = isset($_POST['Keyword'][$lang_id]) ? $_POST['Keyword'][$lang_id] : NULL;
                        $this->_['TitleH1_' . $lang_id] = isset($_POST['TitleH1'][$lang_id]) ? $_POST['TitleH1'][$lang_id] : NULL;
                        $this->_['TitleH2_' . $lang_id] = isset($_POST['TitleH2'][$lang_id]) ? $_POST['TitleH2'][$lang_id] : NULL;
                        $this->_['DescF_' . $lang_id] = isset($_POST['DescF'][$lang_id]) ? $_POST['DescF'][$lang_id] : NULL;

                        $this->_['selected_show_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'show' ? 'selected="true"' : NULL;
                        $this->_['selected_hide_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'hide' ? 'selected="true"' : NULL;
                        $this->_['selected_del_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'del' ? 'selected="true"' : NULL;

                    }
                }

                $noid = isset($this->_seokey[2]) ? intval($this->_seokey[2]) : 0;
                $resultHtml = '';
                $resultHtmlx = '';
                $resultTile = '';

                $html = $this->view->view_display("editor");
                /* Name Block HTML*/
                $this->arrBlockEditor = $this->view->view_get_block_html($html);
                $tabs_title = isset($this->arrBlockEditor['tabs_title']) ? $this->arrBlockEditor['tabs_title'] : Null;
                $tabs_title_ul = isset($this->arrBlockEditor['tabs_title_ul']) ? $this->arrBlockEditor['tabs_title_ul'] : Null;
                $tabs_contents = isset($this->arrBlockEditor['tabs_contents']) ? $this->arrBlockEditor['tabs_contents'] : Null;
                $select_option_com = isset($this->arrBlockEditor['select_option']) ? $this->arrBlockEditor['select_option'] : Null;

                /* SELECT BOX CAT */
                $PID = isset($_POST['PID']) ? $_POST['PID'] : NULL;
                $this->_['option_cat'] = $this->cat_list_box(0 , 0 , $noid , $PID);

                /* SELECT BOX COM */
                $Com = isset($_POST['Com']) ? $_POST['Com'] : NULL;
                $arr_com = $this->mdc_cat_com_box();
                if ( !empty($arr_com) ) {
                    foreach ( $arr_com as $row_com ) {
                        if ( $Com == $row_com['com_value'] ) {
                            $row_com['selected'] = 'selected="true"';
                        } else {

                            $row_com['selected'] = '';
                        }
                        $str_option_com .= $this->view->view_assign($select_option_com , $row_com);
                    }

                    $this->_['option_com'] = $str_option_com;
                }


                /* TAO TAB TITLE */
                if ( !empty($arr_row_titles) ) {
                    foreach ( $arr_row_titles as $arr_row_title ) {
                        $resultTile .= $this->view->view_assign($tabs_title , $arr_row_title);
                    }
                }
                $resultHtml .= $this->view->view_assign($tabs_title_ul , array('tabs_li' => $resultTile));
                /* TAO NOI DUNG TAB */
                if ( !empty($arr_row_titles) ) {
                    foreach ( $arr_row_titles as $arr_row_title ) {
                        $resultHtmlx .= $this->view->view_assign($tabs_contents , $arr_row_title);
                    }
                }
                $resultHtml .= $resultHtmlx;


                /* CHEN MANG NGON NGU DE TAO HTML */
                $result = $this->view->view_assign($resultHtml , $this->_);
                $strhtml = $this->view->view_assign_block($html , array('htmlCat' => $result));

            } /* end permission */
            return $strhtml;
        }

        public function edit_action()
        {
            /* Check permission */
            if ( !$this->per_check_permission(NULL , true) ) {
                $this->_layout = 'error';
                $strhtml = $this->_['not_permission'];

            } else {

                $str_option_com = NULL;
                $this->_['sms'] = '';
                $this->_['dis_none'] = '';

                $catid = isset($this->_seokey[2]) ? intval($this->_seokey[2]) : null;
                if ( $catid ) {

                    /* LAY DU LIEU CAT CUR */
                    $cat_rows = $this->mdc_get_cat($catid);

                    /*Khởi tạo cho cat 2 public*/
                    if ( $catid == 2 ) {
                        $this->_['dis_none'] = ' style="display:none;" ';
                        $_POST['SeoKey'][0] = 'home';
                        $_POST['PID'] = 'home';
                    }

                    /* LAY DANH SACH NGON NGU */
                    $arr_row_titles = $this->mdl_lang_list(); /* lang max */
                    $alang = array(); /* lang data change not empty */
                    $arr_lang_insert = array(); /* lang data ok insert */

                    /* Kiểm tra lang có dữ liệu */
                    if ( !empty($_POST['CatName']) && !empty($arr_row_titles) ) {
                        foreach ( $arr_row_titles as $arr_row ) {
                            $langid = $arr_row['lang_id'];

                            if ( !(empty($_POST['CatName'][$langid]) && empty($_POST['SeoKey'][$langid]) && empty($_POST['Title'][$langid]) && empty($_POST['Desc'][$langid]) && empty($_POST['Keyword'][$langid]) && empty($_POST['TitleH1'][$langid]) && empty($_POST['TitleH2'][$langid]) && empty($_POST['DescF'][$langid]))
                            ) {
                                $alang[] = $langid;
                            }

                        }
                    }

                    /*Kiểm tra lỗi lang có dữ liệu */
                    if ( !empty($_POST['PID']) && !empty($_POST['CatName']) && !empty($_POST['SeoKey']) ) {
                        $add = 0;
                        if ( !empty($alang) ) {
                            foreach ( $alang as $lang_id ) {
                                if ( $catid == 2 ) {
                                    $_POST['SeoKey'][$lang_id] = 'home-' . $lang_id;
                                }
                                $_POST['SeoKey'][$lang_id] = $this->str_seokey($_POST['SeoKey'][$lang_id]);
                                $seo_key = $this->mdc_get_seo_key($_POST['SeoKey'][$lang_id] , $lang_id , $cat_rows['ID']);
                                if ( $seo_key > 0 ) {
                                    $this->_['sms'] = $this->_['check_seo_key'] . ' ' . $arr_row['lang_name'];

                                } else if ( empty($_POST['CatName'][$lang_id]) ) {
                                    $this->_['sms'] = $this->_['empty_cat_name'];

                                } else if ( empty($_POST['SeoKey'][$lang_id]) ) {

                                    $this->_['sms'] = $this->_['empty_seo_key'];

                                } else {
                                    /* Upload pic */
                                    if ( isset($_FILES['Img']) && !empty($_FILES['Img']['name']) ) {
                                        $img = $this->upload_img($_FILES['Img'] , PATH_BASE . '/upload/images/categories/' , $seo_key);
                                        if ( $img ) {
                                            $_POST['Img'] = $img;
                                        } else {
                                            $this->_['sms'] = '';
                                        }
                                    }
                                    $arr_lang_insert[] = $lang_id;
                                }
                            }

                        }
                    }

                    /* Lưu những lang đã kiểm tra */
                    if ( !empty($arr_lang_insert) ) {
                        $add = 0;
                        $id = 0;
                        $done_ok = true;
                        foreach ( $arr_lang_insert as $lagid ) {
                            if ( $add == 0 ) {
                                $rs = $this->mdc_cat_update($cat_rows['ID']);
                                $add++;
                                if ( !$rs ) {
                                    $done_ok = false;
                                }
                            }

                            if ( $rs != 0 && $done_ok == true ) {
                                $done = $this->mdc_cat_update_lang($cat_rows['ID'] , $lang_id);

                                if ( $done == false ) {
                                    $done_ok = false;
                                }
                            }

                        }

                        /* Điều hướng khi tất cả đều thành công */
                        if ( $done_ok == true ) {
                            $this->request->rq_goto_window_parent(URL_BASE . $this->_seokey[0] . '/');
                        }

                    }

                    if ( !empty($cat_rows['Img']) ) {
                        $this->_['path_file_Img'] = urlencode($cat_rows['Img']);
                        $this->_['file_Img'] = URL_BASE . 'upload/images/categories/' . $cat_rows['Img'];
                        $this->_['CatID'] = $cat_rows['ID'];
                        $this->_['dis_none_input'] = 'display:none;';

                    } else {

                        $this->_['file_Img'] = URL_BASE . 'upload/noimg.png';
                        $this->_['dis_none_but'] = 'display:none;';
                    }
                    $this->_['title_editor'] = $this->_['title_editor_edit'];
                    $this->_['val_order'] = isset($_POST['Order']) ? $_POST['Order'] : $cat_rows['Order'];
                    $this->_['checked_horizontal'] = isset($_POST['HorizontalMenu']) && $_POST['HorizontalMenu'] == 1 ? 'checked="true"' : ($cat_rows['HorizontalMenu'] == 1 ? 'checked="true"' : null);
                    $this->_['checked_vertical'] = isset($_POST['VerticalMenu']) && $_POST['VerticalMenu'] == 1 ? 'checked="true"' : ($cat_rows['VerticalMenu'] == 1 ? 'checked="true"' : null);

                    if ( !empty($arr_row_titles) ) {
                        foreach ( $arr_row_titles as $arr_row ) {
                            $lang_id = $arr_row['lang_id'];
                            $cat_lang_row = $this->mdc_get_cat_lang($catid , $lang_id);

                            $this->_['CatName_' . $lang_id] = isset($_POST['CatName'][$lang_id]) ? $_POST['CatName'][$lang_id] : $cat_lang_row['CatName'];
                            $this->_['SeoKey_' . $lang_id] = isset($_POST['SeoKey'][$lang_id]) ? $_POST['SeoKey'][$lang_id] : $cat_lang_row['SeoKey'];
                            $this->_['xTitle_' . $lang_id] = isset($_POST['Title'][$lang_id]) ? $_POST['Title'][$lang_id] : $cat_lang_row['Title'];
                            $this->_['Desc_' . $lang_id] = isset($_POST['Desc'][$lang_id]) ? $_POST['Desc'][$lang_id] : $cat_lang_row['Desc'];
                            $this->_['Keyword_' . $lang_id] = isset($_POST['Keyword'][$lang_id]) ? $_POST['Keyword'][$lang_id] : $cat_lang_row['Keyword'];
                            $this->_['TitleH1_' . $lang_id] = isset($_POST['TitleH1'][$lang_id]) ? $_POST['TitleH1'][$lang_id] : $cat_lang_row['TitleH1'];
                            $this->_['TitleH2_' . $lang_id] = isset($_POST['TitleH2'][$lang_id]) ? $_POST['TitleH2'][$lang_id] : $cat_lang_row['TitleH2'];
                            $this->_['DescF_' . $lang_id] = isset($_POST['DescF'][$lang_id]) ? $_POST['DescF'][$lang_id] : $cat_lang_row['DescF'];

                            $this->_['selected_show_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'show' ? 'selected="true"' : ($cat_lang_row['Status'] == 'show' ? 'selected="true"' : NULL);
                            $this->_['selected_hide_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'hide' ? 'selected="true"' : ($cat_lang_row['Status'] == 'hide' ? 'selected="true"' : NULL);
                            $this->_['selected_del_' . $lang_id] = isset($_POST['Status'][$lang_id]) && $_POST['Status'][$lang_id] == 'del' ? 'selected="true"' : ($cat_lang_row['Status'] == 'del' ? 'selected="true"' : NULL);

                        }
                    }

                    $this->per_check_permission();
                    $this->_layout = 'iframe';
                    $noid = isset($this->_seokey[2]) ? intval($this->_seokey[2]) : 0;
                    $resultHtml = '';
                    $resultHtmlx = '';
                    $resultTile = '';

                    $html = $this->view->view_display("editor");
                    /* Name Block HTML*/
                    $this->arrBlockEditor = $this->view->view_get_block_html($html);
                    $tabs_title = isset($this->arrBlockEditor['tabs_title']) ? $this->arrBlockEditor['tabs_title'] : Null;
                    $tabs_title_ul = isset($this->arrBlockEditor['tabs_title_ul']) ? $this->arrBlockEditor['tabs_title_ul'] : Null;
                    $tabs_contents = isset($this->arrBlockEditor['tabs_contents']) ? $this->arrBlockEditor['tabs_contents'] : Null;
                    $select_option_com = isset($this->arrBlockEditor['select_option']) ? $this->arrBlockEditor['select_option'] : Null;

                    /* SELECT BOX CAT */
                    $PID = isset($_POST['PID']) ? $_POST['PID'] : $cat_rows['PID'];
                    $this->_['option_cat'] = $this->cat_list_box(0 , 0 , $noid , $PID);

                    /* SELECT BOX COM */
                    $Com = isset($_POST['Com']) ? $_POST['Com'] : $cat_rows['Com'];
                    $arr_com = $this->mdc_cat_com_box();
                    if ( !empty($arr_com) ) {
                        foreach ( $arr_com as $row_com ) {
                            if ( $Com == $row_com['com_value'] ) {
                                $row_com['selected'] = 'selected="true"';
                            } else {

                                $row_com['selected'] = '';
                            }
                            $str_option_com .= $this->view->view_assign($select_option_com , $row_com);
                        }

                        $this->_['option_com'] = $str_option_com;
                    }


                    /* TAO TAB TITLE */
                    if ( !empty($arr_row_titles) ) {
                        foreach ( $arr_row_titles as $arr_row_title ) {
                            $resultTile .= $this->view->view_assign($tabs_title , $arr_row_title);
                        }
                    }
                    $resultHtml .= $this->view->view_assign($tabs_title_ul , array('tabs_li' => $resultTile));
                    /* TAO NOI DUNG TAB */
                    if ( !empty($arr_row_titles) ) {
                        foreach ( $arr_row_titles as $arr_row_title ) {
                            $resultHtmlx .= $this->view->view_assign($tabs_contents , $arr_row_title);
                        }
                    }
                    $resultHtml .= $resultHtmlx;


                    /* CHEN MANG NGON NGU DE TAO HTML */
                    $result = $this->view->view_assign($resultHtml , $this->_);
                    $strhtml = $this->view->view_assign_block($html , array('htmlCat' => $result));
                }

            }
            /* per */
            return $strhtml;
        }

        private function cat_list_box( $id , $indent , $noid = 0 , $select )
        {
            $indent += 1;
            $str = '';
            $rr = $this->mdc_cat_list_box($id , $noid);
            if ( !empty($rr) ) {
                foreach ( $rr as $row ) {
                    $arr_row = array('selected' => ($select == $row['ID'] ? 'selected="true"' : NULL) , 'com_value' => $row['ID'] , 'com_text' => str_repeat("&nbsp;&nbsp;" , $indent) . $row['CatName']);
                    $str .= $this->view->view_assign($this->arrBlockEditor['select_option'] , $arr_row);
                    if ( isset($row['ID']) && $str ) {
                        $str .= $this->cat_list_box($row['ID'] , $indent , $noid , $select);
                    }

                }

            }

            return $str;
        }

        public function login_action()
        {
            $this->_content_header = NULL;
            $this->_content_top = NULL;

            $this->_layout = 'login';
            $this->_['error'] = '';

            if ( isset($this->_seokey[2]) && $this->_seokey[2] == $this->_['get_password_key'] ) {
                $this->_['url_base_back'] = URL_BASE . $this->_seokey[0] . "/" . $this->_seokey[1];

                $this->_['but_remember_pass'] = 'but_remember_pass_g';
                $this->_['but_login'] = 'but_login_g';

                if ( isset($_POST['Email']) ) {
                    $user = $this->mdu_get_user_from_email($_POST['Email']);
                    if ( $user["ID"] != "" ) {
                        if(isset($_POST['Captcha']) && isset( $_SESSION['Captcha'])  && ($_POST['Captcha'] == $_SESSION['Captcha']))
                       {
                           $pass = $this->randomPassword(10);
                           $form_mail = $this->createHtmlMail($user , $pass);
                           $this->sentMail( $user["ID"],$user["Email"],$form_mail,$this->_['subject_mail'],$pass);
                           $this->request->rq_goto($this->_['url_base_back']);


                       }
                        else{
                            $this->_['error'] = $this->_['error_captcha'];
                        }
                    }
                    else
                    {
                        $this->_['error'] = $this->_['mail_notexist'];
                    }
                }

                $html = $this->view->view_display('login');
                $arr_block_html = $this->view->view_get_block_html($html);
                $shtml = $this->view->view_assign_block($html , array('Login' => $arr_block_html['login_form']));
                $this->_['url_base'] = URL_BASE;
                $this->_['captcha'] = $arr_block_html['captcha'];
                $this->_['pass_word'] = "";
                $strOkHtml = $this->view->view_assign($shtml , $this->_);
                return $strOkHtml;
            } else {
                $this->_['url_base_back'] = URL_BASE . $this->_seokey[0] . "/" . $this->_seokey[1] . "/" .  $this->_['get_password_key'];
                $this->_['but_remember_pass'] = 'but_remember_pass_a';
                $this->_['but_login'] = 'but_login_a';

                isset($_SESSION['count_login']) ? $_SESSION['count_login'] : $_SESSION['count_login'] = 0;


                if ( isset($_POST['Email']) && isset($_POST['Password']) ) {
                    if ( empty($_POST['Email']) || empty($_POST['Password']) ) {
                        $this->_['error'] = $this->_['error_login_empty'];
                        $_SESSION['count_login'] = $_SESSION['count_login'] + 1;
                    } else if ( !$this->validate_email($_POST['Email']) ) {
                        $this->_['error'] = $this->_['error_email'];
                        $_SESSION['count_login'] = $_SESSION['count_login'] + 1;
                    } else {

                        $row = $this->mdu_login($_POST['Email'] , $_POST['Password']);

                        if ( empty($row) ) {
                            $this->_['error'] = $this->_['error_email_pass'];
                            $_SESSION['count_login'] = $_SESSION['count_login'] + 1;
                        } else {

                            foreach ( $row as $key => $val ) {
                                if ( !empty($val) ) {
                                    $_SESSION[$key] = $val;

                                }
                            }
                        }


                    }

                }

                /* done login */
                /*nhap sai 3 lan co captcha*/
                if ( isset($_POST['Captcha']) && isset($_SESSION['Captcha']) ) {
                    if ( $_POST['Captcha'] == $_SESSION['Captcha'] ) {
                        if ( !empty($_SESSION['ID']) ) {
                            $_SESSION['count_login'] = 0;
                            $this->request->rq_goto(URL_BASE . 'admin/');
                            return false;
                        }
                    } else {
                        $this->_['error'] = $this->_['error_captcha'];
                    }
                    /*khong co captcha*/
                }
                elseif($_SESSION["count_login"]<=3){
                    if ( !empty($_SESSION['ID']) ) {
                        $_SESSION['count_login'] = 0;
                        $this->request->rq_goto(URL_BASE . 'admin/');
                        return false;
                    }
                }
                if ( $_SESSION['count_login'] < 3 ) {
                    $html = $this->view->view_display('login');
                    $arr_block_html = $this->view->view_get_block_html($html);
                    $shtml = $this->view->view_assign_block($html , array('Login' => $arr_block_html['login_form']));
                    $this->_['captcha'] = "";
                    $this->_['pass_word'] = $arr_block_html['pass'];
                    $strOkHtml = $this->view->view_assign($shtml , $this->_);
                    return $strOkHtml;
                } else {
                    $html = $this->view->view_display('login');
                    $arr_block_html = $this->view->view_get_block_html($html);
                    $shtml = $this->view->view_assign_block($html , array('Login' => $arr_block_html['login_form']));
                    $this->_['url_base'] = URL_BASE;
                    $this->_['captcha'] = $arr_block_html['captcha'];
                    $this->_['pass_word'] = $arr_block_html['pass'];
                    $strOkHtml = $this->view->view_assign($shtml , $this->_);
                    return $strOkHtml;
                }
            }


        }
        function randomPassword($number) {
            $alphabet = "a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-w-x-y-z-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-W-X-Y-Z-0-1-2-3-4-5-6-7-8-9";
            $alphabet =explode("-",$alphabet);
            $pass = "";
            for ($i = 0; $i <= $number; $i++) {
                $n = rand(0, count($alphabet)-1);
                $pass.= $alphabet[$n];
            }
            return $pass;
        }
        private function  createHtmlMail($user,$pass)
        {
            $res_html = "";
            $html = $this->view->view_display('mail');
            $arr_block_html = $this->view->view_get_block_html($html);
            $this->_["pass_new_value"] = $pass;
            $user["Fullname"]= strtolower($user["Fullname"]);
            $res_html .= $this->view->view_assign($arr_block_html['content'] , $user);
            $shtml = $this->view->view_assign_block($html , array('MAIL' => $res_html));
            $strOkHtml = $this->view->view_assign($shtml , $this->_);
            return $strOkHtml;
        }

        private function  sentMail($ID, $email , $Html , $subject,$pass)
        {
            $send_mail = new handle_mail();
            $send_mail->protocol = Emailprotocol;
            $send_mail->parameter = "";
            $send_mail->hostname = Emailhostname;
            $send_mail->username = Emailuser;
            $send_mail->password = Emailpassword;
            $send_mail->port = Emailport;
            $send_mail->mail_set_to(trim($email));
            $send_mail->mail_set_from(Emailuser);
            $send_mail->mail_set_sender(Emailuser);
            $send_mail->mail_set_subject(strip_tags(html_entity_decode($subject , ENT_QUOTES , 'UTF-8')));
            $send_mail->setHtml(html_entity_decode($Html , ENT_QUOTES , 'UTF-8'));
            $send_mail->mail_send();
            $pass =  md5(md5($pass));
            $this->mdu_update_pass($pass,$ID);
        }
        public function logout_action()
        {
            session_destroy();
            unset($_SESSION);

            $this->request->rq_goto(URL_BASE . 'admin/' . $this->route->rt_get_seokey_act('login') . '/');
        }

        /* Add vao com act */
        public function show_action()
        {
        }

        public function hide_action()
        {
        }

        public function del_action()
        {
        }

        public function delete_action()
        {
        }

        public function order_action()
        {
        }

        /* End Add vao com act */

        public function index_ajax_action()
        {
            if ( $this->_seokey[2] == "captcha" ) {
                $captcha = new handle_captcha();
                $captcha->captcha_display();
                exit();
            }
            /* Check permission */
            $this->per_check_permission();


            $this->_layout = 'NO_LAYOUT';
            /*
           * module ajax
           *
           */
            $module = isset($this->_seokey[2]) ? $this->_seokey[2] : null;
            $module_name = isset($this->_seokey[3]) ? $this->_seokey[3] : null;
            if ( $module == 'mod' ) {
                foreach ( $this->_seokey as $k => $arr ) {
                    if ( $k > 2 ) {
                        $arr_seo_key[] = $arr;
                    }
                }

                $_controller_module = "controller_module_" . $module_name;
                $module = new $_controller_module();
                $module->_seokey = $this->_seokey;
                $module->mod_seokey = $arr_seo_key;

                print($module->index_ajax());
                exit();
            } /*
		* ajax ctr index
		* Chi co index moi co doan nay.
		*/ else {

                $active = isset($this->_seokey[2]) ? $this->_seokey[2] : null;
                $str_id = isset($this->_seokey[3]) ? $this->_seokey[3] : null;
                $str_path = isset($this->_seokey[4]) ? $this->_seokey[4] : null;

                /* check permission */
                if ( $this->per_check_permission($active) ) {
                    if ( $active == 'delpic' && !empty($str_path) ) {

                        $this->mdc_cat_del_pic($str_id);
                        unlink(PATH_BASE . 'upload/images/categories/' . $str_path);
                    }

                    $done = $this->mdc_tool_func($this->model , $active , $str_id);
                    if ( $done ) {
                        return 'ok';
                    } else {
                        return 'noOk';
                    }
                }
            }
            /* ajax */

        }

    }